#!/usr/bin/env/python3
# -*- coding: utf-8 -*-

"""
@Time    : 2020/3/21 11:46
@Author  : Chen Liu
@FileName: matrix.py
@Software: PyCharm
"""


class Solution:
    def spiralOrder(self, matrix):
        """按层遍历"""
        row = len(matrix)
        col = len(matrix[0])
        res = []

        if row == 1:
            return matrix[0]
        if col == 1:
            for i in range(row):
                res.append(matrix[i][0])
            return res

        min_ele = min(row, col)
        for i in range((min_ele + 1) // 2):
            print(i)
            # 为什么要i>0?---因为当矩阵为[1,2],[3,4]]的时候，也满足这样的条件
            # 当最深层为1行或者一列或者1个数字时
            if i > 0 and row - 1 - i == 1 and col - 1 - i == 1:
                res.append(matrix[i][i])

            elif i > 0 and row - 1 - i == 1:
                res.extend(matrix[i][i:col - i])

            elif i > 0 and col - 1 - i == 1:
                for j in range(i, row - i):
                    print("down: ", matrix[j][i])
                    res.append(matrix[j][i])
            else:
                print("right: ", matrix[i][i:col - i])
                res.extend(matrix[i][i:col - i])

                for j in range(i+1, row-i):
                    print("down: ", matrix[j][col - 1 - i])
                    res.append(matrix[j][col - 1 - i])

                print("left: ", matrix[row - 1 - i][col - 2 - i:i:-1])
                res.extend(matrix[row - 1 - i][col - 2 - i:i:-1])

                for k in range(row-1-i, i, -1):
                    print("up: ", matrix[k][i])
                    res.append(matrix[k][i])

        return res

    # def spiralOrder_2(self, matrix):
    """尝试判断最深层是否是一行或者一列，没有找到统一的规律，失败"""
    #     if not matrix:
    #         return []
    #
    #     row = len(matrix)
    #     col = len(matrix[0])
    #     res = []
    #     min_e = min(row, col)
    #     for i in range((min_e + 1) // 2):
    #         print(i)
    #         if row == 1 or col == 1 or (row % 2 != 0 and col % 2 != 0):
    #             if i == (min_e + 1) // 2 - 1:
    #                 print("res: ", res)
    #                 res.extend(matrix[i][i:col - i])
    #                 for j in range(i+1, row-i):
    #                     res.append(matrix[j][i])
    #
    #                 return res
    #
    #         print("right: ", matrix[i][i:col - i])
    #         res.extend(matrix[i][i:col - i])
    #
    #         for j in range(i + 1, row - i):
    #             print("down: ", matrix[j][col - 1 - i])
    #             res.append(matrix[j][col - 1 - i])
    #
    #         print("left: ", matrix[row - 1 - i][col - 2 - i:i:-1])
    #         res.extend(matrix[row - 1 - i][col - 2 - i:i:-1])
    #
    #         for k in range(row - 1 - i, i, -1):
    #             print("up: ", matrix[k][i])
    #             res.append(matrix[k][i])
    #
    #     return res

    def spiralOrder_2(self, matrix):
        """上面的解法稍微有点拉杂，利用pop函数，抛出遍历过的元素，直到matrix为空"""
        res = []
        while matrix:
            res.extend(matrix.pop(0))
            if matrix and matrix[0]:
                for row in matrix:
                    res.append(row.pop())  # 默认抛出最后一个元素

            if matrix:
                res.extend(matrix.pop()[::-1])

            if matrix and matrix[0]:
                for row in matrix[::-1]:
                    res.append(row.pop(0))

        return res


if __name__ == "__main__":
    s = Solution()
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    # matrix = [[3], [2]]
    # matrix = [[1, 2, 3]]
    matrix = [[2, 3, 4], [5, 6, 7], [8, 9, 10], [11, 12, 13]]
    # matrix = [[1, 2], [3, 4]]
    # matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
    # matrix = [[3], [2]]
    res = s.spiralOrder_2(matrix)
    print("res: ", res)
